
function loadUMDModule(url, name) {
  
  return new Promise((resolve, reject) => {
    if(window[name]){
      resolve(name?window[name]:'');
      return
    }
    const script = document.createElement('script');
    script.type = 'text/javascript';
    script.async = true;
    script.src = url;
    script.onload = () => {
      resolve(name?window[name]:'');
    };
    script.onerror = reject;
    document.head.appendChild(script);
  });
}

const loadComponent = async code => {
  return new Promise(resolve => {
    const resourceURL = TeldWebConfig.ResourceURL;
    loadUMDModule(`${resourceURL}/tff/component/pc/umd/${code}.js`, code).then(
      component => {
        resolve();
        window.fasAppInstance.component(code,component.default);
        window.fasAppInstance.use(component.default);
        
      },
    ).catch(()=>{
      resolve();
    })
  });
};

//http://resource.teld.org/tff/component/pc/umd/TComponents.deps.js
export const loadPortal = async (url,code, depComponent) => {
  //调试阶段不需要注入依赖
  if(!window.TQuasarBase){
    depComponent=[]
  }else{
    if(!window.TComponentDeps ){
      await loadUMDModule(`${TeldWebConfig.ResourceURL}/tff/component/pc/umd/TComponents.deps.js`,'TComponentDeps')
    }
  }
  if(!depComponent){
    depComponent=[]
  }
  if(window.TQuasarBase){
    depComponent.push('TDataGrid','TLabel')
  }
  const promiseList = [];
  for(let componentName of  depComponent){
    if(window.TComponentDeps[componentName]){
      for(let item of window.TComponentDeps[componentName]){
        if(item.indexOf(['vue-i18n'])==-1){
          await loadUMDModule(`//resource.teld.cn/fas/${item}.min.js`)
        }
        
      }
      // window.TComponentDeps[componentName].forEach(item=>{
      //   promiseList.push( loadUMDModule(`//resource.teld.cn/fas/${item}.min.js`));
      // })
    }
    await loadComponent(componentName)
  }
  // depComponent.forEach(componentName => {
    
    
  //   promiseList.push(new Promise((res)=>{
  //     setTimeout(() => {
  //       res(1)
  //     }, 100);
  //   }))
  // });
  // await Promise.all(promiseList);
  if(!url){
    let resourceURL =TeldWebConfig.ResourceURL
     if(!window.TQuasarBase){
       resourceURL='//resource.teld.org'
    }
     return new Promise(resolve=>{
       loadUMDModule(
         `${resourceURL}/tff/fas/${code.replace(/-/ig,'').replace(/_/ig,'')}.js`,
         code,
       ).then(component => {
         window.fasAppInstance.component(code.toLowerCase().replace(/.umd/ig,''),component.default);
         window.fasAppInstance.use(component.default);
         resolve(component.default);
       }).catch(()=>{
        resolve(null);
      })
     })
   
  }
  return new Promise(resolve => {
    const resourceURL = TeldWebConfig.ResourceURL;
    loadUMDModule(
      // `${resourceURL}/tff/component/common/umd/${code}.umd.js`,
      `${resourceURL}/${url}`,
      code,
    ).then(component => {
      window.fasAppInstance.component(code.toLowerCase(),component.default);
      window.fasAppInstance.use(component.default);
      resolve(component.default);
    }).catch(()=>{
      resolve(null);
    })
  });
};

//测试先使用     loadPortal('demo', ['TButton', 'TCard']);


